using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.Threading;
using C6820;
using System.Collections;

namespace MFConsoleC6820
{
    /// <summary>
    /// Test for C6820 class
    /// </summary>
    public class Program
    {
        private static C6820.C6820 camera;
        private static ushort filecount;
        public static void Main()
        {
            bool tag;
            camera = new C6820.C6820(new SerialPort.Configuration(SerialPort.Serial.COM1, SerialPort.BaudRate.Baud115200, false));
            tag = camera.Sync();//test sync
            Debug.Print(tag.ToString());
            Thread.Sleep(500);

            tag = camera.SetBaudRate(C6820.C6820BaudRate.Baud115200);//test baudRate
            Debug.Print(tag.ToString());
            Thread.Sleep(500);

            tag = camera.SetOperationMode(C6820.OperationMode.Playback);//test camera operation mode
            Debug.Print(tag.ToString());
            Thread.Sleep(500);

            HardwareRevision hr = camera.GetRevisionID();
            Debug.Print("HWVersion: " + hr.HardwareVersion.ToString());
            Debug.Print("Coach Version: " + hr.CoachVersion.ToString());
            Debug.Print("Sersor Version: " + hr.SensorVersion.ToString());
            Debug.Print("HCE Version: " + hr.HCEVersion.ToString());
            Thread.Sleep(100);

            StorageMediaInfo sm = camera.GetStorageMediaInfo();
            Debug.Print("Storage Available Space: " + sm.AvailableSpace.ToString());
            Debug.Print("Storage available avi time: " + sm.AviAvailableTime.ToString() + " seconds");
            filecount = sm.FileCount;

            Debug.Print("Storage filecount: " + filecount.ToString());
            Debug.Print("Storage Pictureleft: " + sm.PictureLeft.ToString());
            Thread.Sleep(100);

            tag = camera.SetSystemClock(2008, 7, 28, 13, 1, 1);//test to set system clk
            Debug.Print(tag.ToString());
            Thread.Sleep(500);

            tag = camera.SetJpegResolution(C6820.JpegResolution.R1280x960, 24);//test to set jpeg resolution
            Debug.Print(tag.ToString());
            Thread.Sleep(500);

            tag = camera.SetDateTimeStamp(new DateTimeStamp(DateFormat.YMD, DateCorner.BottomRight, DateStyle.DateAndTime), true);//test to set datetime stamp
            Debug.Print(tag.ToString());
            Thread.Sleep(500);

            tag = camera.SetStringStamp(new StringStamp(80, 90, 160, 150, 0, 0, 255, 4, "CSIP"), true);//test to set string stamp
            Debug.Print(tag.ToString());
            Thread.Sleep(500);

            DateTime dt = camera.GetSystemTime();
            Debug.Print("System time of jpeg module: " + dt.ToString("yyyyMMdd"));

            camera.ConfigSnapshot(WhiteBalance.Normal, EV.EV04, Contrast.Normal, ColorEffect.Normal, Sharpness.Normal);
            Debug.Print(tag.ToString());
            Thread.Sleep(500);

            Debug.Print("Luminance: " + camera.GetLuminance().ToString());


            //  tag = camera.CaptureJpeg(1);
            //  Debug.Print("JpegCapture: " + tag.ToString());
            // Thread.Sleep(500);

            camera.Sync();
            Thread.Sleep(500);
            tag = camera.SetAVIResolution(AVIResolution.R640x480, 24);
            Debug.Print(tag.ToString());
            Thread.Sleep(500);

            #region comment

            //Debug.Print("AVI begin :" + TestAVI(true).ToString());
            //Thread.Sleep(3000);

            //Debug.Print("AVI End :" + TestAVI(false).ToString());
            //Thread.Sleep(500);


            //FileInfo fi = camera.GetFileInfo(2);
            //Debug.Print(" Filesize: " + fi.FileSize.ToString() + " Byte");
            //Debug.Print(" Filename: " + fi.FileName);
            //if (fi.VideoLength != 0)
            //{
            //    Debug.Print(" File video length: " + fi.VideoLength.ToString());
            //}

            //camera.SelectParticularFile(2);

            //Thread.Sleep(50);
            //camera.SetOperationMode(OperationMode.Playback);
            //camera.Sync();
            //OperationMode mo2 = camera.GetOperationMode();
            //Debug.Print(mo2.ToString());
            //Debug.Print("Play AVI: " + camera.PlayCurrentAVI(PlayBackOperation.Play).ToString());
            //camera.SetOperationMode(OperationMode.Playback);
            //Thread.Sleep(50);
            //PlayAVIList();

            #endregion
        }


        public static bool TestAVI(bool start)
        {
            int i = 0;
            bool tag = true;
            while (!camera.RecordAVI(start))
            {
                i++;
                if (i > 60)
                {
                    tag = false;
                    break;
                }
            }
            return tag;
        }

        private static void PlayAVIList()
        {
            FileInfo fi;
            for (ushort i = 1; i<filecount; i++)
            {
                fi = camera.GetFileInfo(i);
                if (fi.FileName.IndexOf("AVI") > 0)
                {
                    camera.SelectParticularFile(i);
                    camera.PlayCurrentAVI(PlayBackOperation.Play);
                }
            }
        }
    }
}
